package org.facyt.vacas.services.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;

public class VacasAuthenticationProvider implements AuthenticationProvider {

	private UsuarioService userService;

	public UsuarioService getUserService() {
		return userService;
	}

	public void setUserService(UsuarioService userService) {
		this.userService = userService;
	}

	public Authentication authenticate(Authentication auth)
			throws AuthenticationException {

		String principal = (String) auth.getPrincipal();
		String password = (String) auth.getCredentials();

		// if ()

		// if (SIMPLE_USERS.containsKey(auth.getPrincipal())
		// &&
		// SIMPLE_USERS.get(auth.getPrincipal()).equals(auth.getCredentials()))
		// {
		//
		// }
		// throw new
		// BadCredentialsException("Username/Password does not match for " +
		// auth.getPrincipal());
		// }

		List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

		authorities.add(new GrantedAuthorityImpl("ROLE_USER"));

		return new UsernamePasswordAuthenticationToken(auth.getName(),
				auth.getCredentials(), authorities);
	}

	public boolean supports(Class<?> arg0) {
		return (VacasAuthenticationProvider.class.isAssignableFrom(arg0));
	}

}
